package cn.yinyu.queue.module.ecg.service.call;

import cn.yinyu.queue.module.ecg.dal.dataobject.queue.QueueDO;
import cn.yinyu.queue.module.ecg.dal.dataobject.room.RoomDO;
import cn.yinyu.queue.module.ecg.dal.mysql.room.RoomMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;

import cn.yinyu.queue.module.ecg.controller.admin.call.vo.*;
import cn.yinyu.queue.module.ecg.dal.dataobject.call.CallDO;
import cn.yinyu.queue.framework.common.pojo.PageResult;
import cn.yinyu.queue.framework.common.util.object.BeanUtils;

import cn.yinyu.queue.module.ecg.dal.mysql.call.CallMapper;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static cn.yinyu.queue.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.yinyu.queue.module.ecg.enums.ErrorCodeConstants.*;

/**
 * 叫号 Service 实现类
 *
 * @author 马剑波
 */
@Service
@Validated
public class CallServiceImpl implements CallService {

    @Resource
    private CallMapper callMapper;

    @Resource
    private RoomMapper roomMapper;

    @Override
    public Integer createCall(CallSaveReqVO createReqVO) {
        // 插入
        CallDO call = BeanUtils.toBean(createReqVO, CallDO.class);
        callMapper.insert(call);
        // 返回
        return call.getId();
    }

    @Override
    public void updateCall(CallSaveReqVO updateReqVO) {
        // 校验存在
        validateCallExists(updateReqVO.getId());
        // 更新
        CallDO updateObj = BeanUtils.toBean(updateReqVO, CallDO.class);
        callMapper.updateById(updateObj);
    }

    @Override
    public void deleteCall(Integer id) {
        // 校验存在
        validateCallExists(id);
        // 删除
        callMapper.deleteById(id);
    }

    private void validateCallExists(Integer id) {
        if (callMapper.selectById(id) == null) {
            throw exception(CALL_NOT_EXISTS);
        }
    }

    @Override
    public CallDO getCall(Integer id) {
        return callMapper.selectById(id);
    }

    @Override
    public CallDO getNextCall() {
        CallDO callDO = callMapper.getNextCall(); // 大屏
        return callDO;
    }

    @Override
    public CallDO getNextInstallCall(String reqIp) {
        List<RoomDO> roomDOList = roomMapper.queueByIp(reqIp);
        Optional<RoomDO> optionalQueueDO = roomDOList.stream().filter(item -> StringUtils.hasLength(item.getIp())).findFirst();
        if (!optionalQueueDO.isPresent()) {
            return  null;
        }

        Long roomId = optionalQueueDO.get().getRoomId();
        CallDO callDO = callMapper.getNextInstallCall( roomId ); // 诊间屏
        return callDO;
    }

    @Override
    public PageResult<CallDO> getCallPage(CallPageReqVO pageReqVO) {
        return callMapper.selectPage(pageReqVO);
    }

    @Override
    public Integer callAgain(CallSaveReqVO createReqVO) {
        CallDO callDO = callMapper.getLatestPatientCall(createReqVO.getPatId());
        if (null != callDO && callDO.getCalled() == 0)
            return 0;

        return createCall(createReqVO);
    }
}
